package io.renren.modules.study.entity;

import cn.jiguang.common.utils.StringUtils;
import net.sf.ezmorph.object.AbstractObjectMorpher;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimestampToDateMorpher extends AbstractObjectMorpher {
    private static String DATE_TIME_FORMAT  = "MM-dd-yyyy HH:mm:ss";
    static SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);

    static TimeZone pstTimeZone = TimeZone.getTimeZone("PST");


    public Object morph(Object value) {
        if(value instanceof String) {
            String s = (String)value;
            return convertStringToTimestamp(s);
        }
        return null;
    }

    public Class morphsTo() {
        return Date.class;
    }

    public boolean supports(Class clazz) {
//	return clazz.equals(String.class);
        return String.class.isAssignableFrom( clazz );
    }

    private  Date  convertStringToTimestamp(String input){

        if(!StringUtils.isEmpty(input)){
            try {
                return dateTimeFormat.parse(input);
            } catch (ParseException e) {
            }
        }

        return null;
    }
}
